Skip to content

add goal for Complex<T> (for FFI) - #747

Merged
nxsaken merged 4 commits into
rust-lang:mainfrom
folkertdev:interop-complex
Aug 19, 2026
Merged

add goal for Complex<T> (for FFI)#747
nxsaken merged 4 commits into
rust-lang:mainfrom
folkertdev:interop-complex

Conversation

@folkertdev

@folkertdev folkertdev commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Today there are still a number of cases where Rust cannot, in a portable way, express type signatures that C can define.

The C _Complex type is one such missing piece. This type is conceptually just

#[repr(C)]
struct Complex<T> { 
    real: T, 
    imaginary: T 
}

But many ABI pass Complex<{float}> and Complex<{int}> in a custom way, different from the equivalent C struct. The value of Complex<T> as a built-in type is that we can match those ABIs.

use core::ffi::c_double;

unsafe extern "C" {
    // Complex square root provided by libm.
    safe fn csqrtf(_: Complex<c_float>) -> Complex<c_float>;
}

fn main() {
    let c = Complex::new(-1.0, 0.0);
    assert_eq!(csqrtf(c), Complex::new(0.0, 1.0));
}

There is an accepted RFC (RFC 3892) for Complex<T>.

Rendered

@nxsaken nxsaken added the C-goal-proposal This is a goal proposal label Aug 3, 2026
@rustbot

rustbot commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

A zulip topic was opened to discuss this issue.

Comment thread src/2026/interop-complex.md Outdated
Co-authored-by: Erik Jonkers <erik@tweedegolf.com>
Comment thread src/2026/interop-complex.md Outdated
@folkertdev

Copy link
Copy Markdown
Contributor Author

Apparently T-compiler members can self-champion goals. So, I'll be the compiler champion for this goal.

Comment thread src/2026/interop-complex.md
@nxsaken
nxsaken merged commit e57a52d into rust-lang:main Aug 19, 2026
2 checks passed
@github-project-automation github-project-automation Bot moved this from To Do to Done in Triage Queue Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-goal-proposal This is a goal proposal

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants